/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.ucm;

import assignment_cp3120.dataObjects.AllLocations;
import assignment_cp3120.manager.editflights.EditFlightsModel;
import assignment_cp3120.manager.editflights.EditFlightsView;
import java.sql.Date;
import java.sql.Time;

/**
 * PRECONDITION: user has entered flight details and pressed edit button
 * 1.System takes flights from view and puts them in the model
 * 2.System deletes old flight from database
 * 3.System adds edited flight to database
 * 4.System clears the view
 *
 * POSTCONDITION: flight added to database
 *
 * @author Richard Irons
 */
public class UCEditFlights implements UseCaseController {
    private EditFlightsView view;
    private EditFlightsModel model;

    public UCEditFlights() {
        this(new EditFlightsView(), new EditFlightsModel());
    }

    public UCEditFlights(EditFlightsView view, EditFlightsModel model) {
        this.view = view;
        this.model = model;
    }
            
    public void run() {
        //* 1.System takes flights from view and puts them in the model
        viewToModel();
        //* 2.System deletes old flight from database
        model.deleteEditFlight();
        //* 3.System adds edited flight to database
        model.addFlight();
        //* 4.System clears the view
        view.clearView();
    }
    private void viewToModel(){
        model.setEditingFlight(view.getSelectedFlight());
        model.setFlightID(view.getSelectedFlight().getFlight_id());
        model.setDepID(AllLocations.getLocationIDFromName(view.getDepName()));
        model.setDestID(AllLocations.getLocationIDFromName(view.getDestName()));
        model.setDate(Date.valueOf(view.getDate()));
        model.setArrTime(Time.valueOf(view.getArrTime()));
        model.SetDepTime(Time.valueOf(view.getDepTime()));
        model.setFlightCapacity(view.getCapacity());
        model.setFlightPrice(view.getPrice());
    }
}
